package com.vhly.tuite.twclient.model;

import com.vhly.tuite.twclient.util.DOMUtil;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-3-3
 * Time: 17:43:46
 */
public class User implements Model {
    public String id;
    public String name;
    public String screen_name;
    public String location;
    public String description;
    public String profile_image_url;
    public String url;
    public boolean protect;
    public int followers_count;
    public int bgcolor;
    public int text_color;
    public int link_color;
    public int sidebar_fill_bar;
    public int sidebar_border_color;
    public int friends_count;
    public String created_at;
    public int favourites_count;
    public String background_image_url;
    public int statuses_count;
    public boolean verified;

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("User: [");
        sb.append(name).append(" id:").append(id).append(" location: ").append(location);
        sb.append(']');
        return sb.toString();
    }

    public void parse(Element el) {
        if (el != null) {
            NodeList nl = el.getElementsByTagName("id");
            Element edl;
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                id = DOMUtil.getTextFromElement(edl);
            }

            nl = el.getElementsByTagName("name");
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                name = DOMUtil.getTextFromElement(edl);
            }

            nl = el.getElementsByTagName("screen_name");
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                screen_name = DOMUtil.getTextFromElement(edl);
            }


            nl = el.getElementsByTagName("location");
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                location = DOMUtil.getTextFromElement(edl);
            }


            nl = el.getElementsByTagName("description");
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                description = DOMUtil.getTextFromElement(edl);
            }


            nl = el.getElementsByTagName("profile_image_url");
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                profile_image_url = DOMUtil.getTextFromElement(edl);
            }

            nl = el.getElementsByTagName("url");
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                url = DOMUtil.getTextFromElement(edl);
            }

            nl = el.getElementsByTagName("protect");
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                String str = DOMUtil.getTextFromElement(edl);
                protect = str != null && str.equals("protect");
            }
            nl = el.getElementsByTagName("followers_count");
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                followers_count = Integer.parseInt(DOMUtil.getTextFromElement(edl));
            }
            nl = el.getElementsByTagName("friends_count");
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                friends_count = Integer.parseInt(DOMUtil.getTextFromElement(edl));
            }
            nl = el.getElementsByTagName("statuses_count");
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                statuses_count = Integer.parseInt(DOMUtil.getTextFromElement(edl));
            }
            nl = el.getElementsByTagName("created_at");
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                created_at = DOMUtil.getTextFromElement(edl);
            }
            nl = el.getElementsByTagName("background_image_url");
            if (nl != null && nl.getLength() > 0) {
                edl = (Element) nl.item(0);
                background_image_url = DOMUtil.getTextFromElement(edl);
            }
        }
    }
}
